package com.example.ceramics.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;

import com.example.ceramics.R;
import com.example.ceramics.adapter.MyPagerAdapter1;
import com.example.ceramics.fragment.CartFragment;
import com.example.ceramics.fragment.HomeFragment;
import com.example.ceramics.fragment.MineFragment;
import com.example.ceramics.fragment.SortFragment;

import java.util.ArrayList;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class TestActivity extends AppCompatActivity implements VerticalTabLayout.OnTabSelectedListener, ViewPager.OnPageChangeListener{

    private VerticalTabLayout mTab;
    private ViewPager mVp;
    private ArrayList<String> mTitlesList;
    private ArrayList<Fragment> mFragmentsList;
    private static final String TAG = "MainActivity----";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        initView();
        initData();
    }

    private void initData() {
        mTab.setTabAdapter(new TabAdapter() {
            @Override
            public int getCount() {
                return mTitlesList.size();
            }

            @Override
            public ITabView.TabBadge getBadge(int position) {
                return null;
            }

            @Override
            public QTabView.TabIcon getIcon(int position) {
                return null;
            }

            @Override
            public QTabView.TabTitle getTitle(int position) {
                QTabView.TabTitle title = new QTabView.TabTitle.Builder()
                        .setContent(mTitlesList.get(position))//设置数据   也有设置字体颜色的方法
                        .build();
                return title;
            }

            @Override
            public int getBackground(int position) {
                return R.drawable.shape_login_form;//选中的背景颜色
            }
        });
    }

    private void initView() {
        mTab = (VerticalTabLayout) findViewById(R.id.tab);
        mVp = (ViewPager) findViewById(R.id.vp);

        mTitlesList = new ArrayList<>();//初始化Tab栏数据
        mTitlesList.add("小票设置");
        mTitlesList.add("语音播报设置");
        mTitlesList.add("安全密码设置");
        mTitlesList.add("发票设置");

        mFragmentsList = new ArrayList<>();//添加碎片
        mFragmentsList.add(new HomeFragment());
        mFragmentsList.add(new SortFragment());
        mFragmentsList.add(new CartFragment());
        mFragmentsList.add(new MineFragment());

        //创建适配器、适配器继承自FragmentPagerAdapter
        MyPagerAdapter1 myPagerAdapter = new MyPagerAdapter1(getSupportFragmentManager(), mFragmentsList);
        mVp.setAdapter(myPagerAdapter);//绑定适配器
        //Tab和ViewPager相互联动
        mVp.setOnPageChangeListener(this);
        mTab.addOnTabSelectedListener(this);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        mTab.setTabSelected(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onTabSelected(TabView tab, int position) {
        mVp.setCurrentItem(position);
    }

    @Override
    public void onTabReselected(TabView tab, int position) {

    }
}